package com.breeze.blockchain.auth;

import com.breeze.blockchain.mybatis.entity.AdminDo;
import com.breeze.blockchain.mybatis.entity.UserDo;
import java.util.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author breeze
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AuthContextHolder {

  public static final ThreadLocal<AdminDo> SYS_USER = new InheritableThreadLocal<>();
  public static final ThreadLocal<UserDo> APP_USER = new InheritableThreadLocal<>();

  public static Long mybatisGetUserId() {
    if (SYS_USER.get() != null) {
      return SYS_USER.get().getId();
    } else if (APP_USER.get() != null) {
      return APP_USER.get().getId();
    }
    return 0L;
  }

  public static Long getAppLoginUserId() {
    return Optional.ofNullable(APP_USER.get()).map(UserDo::getId).orElse(null);
  }

  public static Long getAdminLoginUserId() {
    return Optional.ofNullable(SYS_USER.get()).map(AdminDo::getId).orElse(null);
  }

  public static UserDo getAppLoginUser() {
    return APP_USER.get();
  }

  public static AdminDo getAdminLoginUser() {
    return SYS_USER.get();
  }
}
